unit PendulumSettingsGUI;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, PendulumViewUtils;

type

  
  TPendulumSettings = class(TForm)
    Colours: TGroupBox;
    Label1: TLabel;
    pbBackground: TPaintBox;
    pbBob: TPaintBox;
    Label2: TLabel;
    pbString: TPaintBox;
    Label3: TLabel;
    pbShortTick: TPaintBox;
    Label4: TLabel;
    pbAngleArc: TPaintBox;
    Label5: TLabel;
    Label6: TLabel;
    pbVelocityArrow: TPaintBox;
    pbAnglePoint: TPaintBox;
    Label7: TLabel;
    Label8: TLabel;
    pbLongTick: TPaintBox;
    GroupBox1: TGroupBox;
    Label9: TLabel;
    edBobDiameter: TEdit;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    edStringWidth: TEdit;
    Label13: TLabel;
    Label14: TLabel;
    edScale: TEdit;
    GroupBox2: TGroupBox;
    Label15: TLabel;
    Label16: TLabel;
    edShortTick: TEdit;
    edLongTick: TEdit;
    Label17: TLabel;
    Label18: TLabel;
    edTickDist: TEdit;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    edLInterval: TEdit;
    edSInterval: TEdit;
    Label23: TLabel;
    Label24: TLabel;
    Button1: TButton;
    Button2: TButton;
    ColorDialog1: TColorDialog;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure pbBackgroundPaint(Sender: TObject);
    procedure pbBobPaint(Sender: TObject);
    procedure pbStringPaint(Sender: TObject);
    procedure pbShortTickPaint(Sender: TObject);
    procedure pbLongTickPaint(Sender: TObject);
    procedure pbAnglePointPaint(Sender: TObject);
    procedure pbVelocityArrowPaint(Sender: TObject);
    procedure pbAngleArcPaint(Sender: TObject);
    procedure pbBackgroundClick(Sender: TObject);
    procedure pbBobClick(Sender: TObject);
    procedure pbStringClick(Sender: TObject);
    procedure pbShortTickClick(Sender: TObject);
    procedure pbLongTickClick(Sender: TObject);
    procedure pbAnglePointClick(Sender: TObject);
    procedure pbVelocityArrowClick(Sender: TObject);
    procedure pbAngleArcClick(Sender: TObject);
  private
    { Private declarations }
	_backgroundColor : ^TColor;
	_bobColor : ^TColor;
	_stringColor : ^TColor;
	_shortTickColor : ^TColor;
	_longTickColor : ^TColor;
	_anglePointerColor : ^TColor;
	_velocityArrowColor : ^TColor;
	_angleArcColor : ^TColor;
	
	_bobDiameter : ^Integer;
	_stringWidth : ^Integer;
	_shortTickLength : ^Integer;
	_longTickLength : ^Integer;
	_tickDistance : ^Integer;
	
	_scale : ^Real;
	_shortTickInterval : ^Integer;
	_longTickInterval : ^Integer;
	

	
	procedure fillPaintbox(PaintBox : TPaintBox; Color : TColor);
	procedure chooseColor(PaintBox: TPaintBox);
	
  public
    { Public declarations }
	InitialiseMethod : TMethod;
	
	Constructor CreateWithSettings(AOwner: TComponent; var BackgroundColor, BobColor, StringColor, ShortTickColor, LongTickColor, AnglePointerColor, VelocityArrowColor, AngleArcColor : TColor; 
									var BobDiameter, StringWidth, ShortTickLength, LongTickLength, TickDistance, ShortTickInterval, LongTickInterval : Integer; 
									var Scale : Real);
	
	procedure UpdateBoxes;
	

  end;



implementation

{$R *.DFM}

	Constructor TPendulumSettings.CreateWithSettings(AOwner: TComponent; var BackgroundColor, BobColor, StringColor, ShortTickColor, LongTickColor, AnglePointerColor, VelocityArrowColor, AngleArcColor : TColor; 
									var BobDiameter, StringWidth, ShortTickLength, LongTickLength, TickDistance, ShortTickInterval, LongTickInterval : Integer; 
									var Scale : Real);
	begin
		Create(AOwner);
		_backgroundColor := @BackgroundColor;
		_bobColor := @BobColor;
		_stringColor := @StringColor;
		_shortTickColor := @ShortTickColor;
		_longTickColor := @LongTickColor;
		_anglePointerColor := @AnglePointerColor;
		_velocityArrowColor := @VelocityArrowColor;
		_angleArcColor := @AngleArcColor;
		
		_bobDiameter := @BobDiameter;
		_stringWidth := @StringWidth;
		_shortTickLength := @ShortTickLength;
		_longTickLength := @LongTickLength;
		_tickDistance := @TickDistance;
		
		_scale := @Scale;
		_shortTickInterval := @ShortTickInterval;
		_longTickInterval := @LongTickInterval;
		
		UpdateBoxes;
	end;
	
	procedure TPendulumSettings.fillPaintbox(PaintBox : TPaintBox; Color : TColor);
	begin
		PaintBox.Canvas.Brush.Color := Color;
		PaintBox.Canvas.FillRect(PaintBox.ClientRect);
	end;
	
	procedure TPendulumSettings.UpdateBoxes;
	begin
		pbBackground.Color := _backgroundColor^;
		pbString.Color := _stringColor^;
		pbBob.Color := _bobColor^;
		pbShortTick.Color := _shortTickColor^;
		pbLongTick.Color := _longTickColor^;
		pbAnglePoint.Color := _anglePointerColor^;
		pbVelocityArrow.Color := _velocityArrowColor^;
		pbAngleArc.Color := _angleArcColor^;
		
		edBobDiameter.Text := intToStr(_bobDiameter^);
		edStringWidth.Text := intToStr(_stringWidth^);
		edShortTick.Text := intToStr(_shortTickLength^);
		edLongTick.Text := intToStr(_longTickLength^);
		edTickDist.Text := intToStr(_tickDistance^);
		
		edScale.Text := floatToStr(_scale^);
		edSInterval.Text := intToStr(_shortTickInterval^);
		edLInterval.Text := intToStr(_longTickInterval^);
	end;
	
procedure TPendulumSettings.Button2Click(Sender: TObject);
begin
Self.Close;
end;

procedure TPendulumSettings.Button1Click(Sender: TObject);
var mbResult : Integer;
begin
	_backgroundColor^ := pbBackground.Color;
	_stringColor^ := pbString.Color;
	_bobColor^ := pbBob.Color;
	_shortTickColor^ := pbShortTick.Color;
	_longTickColor^ := pbLongTick.Color;
	_anglePointerColor^ := pbAnglePoint.Color;
	_velocityArrowColor^ := pbVelocityArrow.Color;
	_angleArcColor^ := pbAngleArc.Color;

        mbResult := 0;
	
	try
		_bobDiameter^ := strToInt(edBobDiameter.Text);
		_stringWidth^ := strToInt(edStringWidth.Text);
		_shortTickLength^ := strToInt(edShortTick.Text);
		_longTickLength^ := strToInt(edLongTick.Text);
		_tickDistance^ := strToInt(edTickDist.Text);
		
		_scale^ := strToFloat(edScale.Text);
		_shortTickInterval^ := strToInt(edSInterval.Text);
		_longTickInterval^ := strToInt(edLInterval.Text);
	except
		on E : Exception do
		begin
			mbResult := Application.MessageBox(PChar('Description: '+E.Message), PChar('Exception Raised: '+E.ClassName), MB_RETRYCANCEL+MB_ICONEXCLAMATION);
		end;
	end;
	
	if (mbResult <> IDRETRY) then
	begin
	if Assigned(InitialiseMethod) then InitialiseMethod;
	Self.Close;
	end;
end;

procedure TPendulumSettings.pbBackgroundPaint(Sender: TObject);
begin
	fillPaintbox(pbBackground, pbBackground.Color);
end;

procedure TPendulumSettings.pbBobPaint(Sender: TObject);
begin
	fillPaintbox(pbBob, pbBob.Color);
end;

procedure TPendulumSettings.pbStringPaint(Sender: TObject);
begin
	fillPaintbox(pbString, pbString.Color);
end;

procedure TPendulumSettings.pbShortTickPaint(Sender: TObject);
begin
	fillPaintbox(pbShortTick, pbShortTick.Color);
end;

procedure TPendulumSettings.pbLongTickPaint(Sender: TObject);
begin
	fillPaintbox(pbLongTick, pbLongTick.Color);
end;

procedure TPendulumSettings.pbAnglePointPaint(Sender: TObject);
begin
	fillPaintbox(pbAnglePoint, pbAnglePoint.Color);
end;

procedure TPendulumSettings.pbVelocityArrowPaint(Sender: TObject);
begin
	fillPaintbox(pbVelocityArrow, pbVelocityArrow.Color);
end;

procedure TPendulumSettings.pbAngleArcPaint(Sender: TObject);
begin
	fillPaintbox(pbAngleArc, pbAngleArc.Color);
end;

procedure TPendulumSettings.pbBackgroundClick(Sender: TObject);
begin
	chooseColor(pbBackground);
end;

procedure TPendulumSettings.pbBobClick(Sender: TObject);
begin
	chooseColor(pbBob);
end;

procedure TPendulumSettings.pbStringClick(Sender: TObject);
begin
	chooseColor(pbString);
end;

procedure TPendulumSettings.pbShortTickClick(Sender: TObject);
begin
	chooseColor(pbShortTick);
end;

procedure TPendulumSettings.pbLongTickClick(Sender: TObject);
begin
	chooseColor(pbLongTick);
end;

procedure TPendulumSettings.pbAnglePointClick(Sender: TObject);
begin
	chooseColor(pbAnglePoint);
end;

procedure TPendulumSettings.pbVelocityArrowClick(Sender: TObject);
begin
	chooseColor(pbVelocityArrow);
end;

procedure TPendulumSettings.pbAngleArcClick(Sender: TObject);
begin
	chooseColor(pbAngleArc);
end;

procedure TPendulumSettings.chooseColor(PaintBox: TPaintBox);
begin
	ColorDialog1.Color := PaintBox.Color;
	if ColorDialog1.Execute then
	begin
		PaintBox.Color := ColorDialog1.Color;
	end;
end;

end.
